<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/1
 * Time: 10:00
 */
namespace app\index\controller;

use think\Db;

class Postsettlement extends Common{
	
	//判断是否登录
	public function _initialize()
	{
		parent::_initialize();
	}
	
	/**
	 * 后结算的商家
	 * @param string get.title 要检索的字段
	*/
	public function index()
	{
		$where = ['query'=>[]];
		$db = Db::table('postpayment')
				->field('business.id,business.name,business.email,business.password,business.linkman,
						business.telephone,business.paymentaccount,business.settlementdate,business.bank,
						business.branch,business.token,business.account')
				->join('business','business.id = postpayment.bid','LEFT')
				->where('postpayment.status','<>',4);
		if(!empty(trim(input('get.title')))){
			$title = trim(input('get.title'));
			$db->where('business.account|business.linkman|business.telephone','like',"%{$title}%");
			$where['query']['title'] = $title;
		}
		$result = $db->group('business.email')->paginate(8,false,$where);
		return view('index',compact('result','title'));
	}
	
	/**
	 * 后结算的商家订单
	 * @param string get.bid 商家id
	 * @param string get.ordernumber 订单号
	*/
	public function order()
	{
		$where = ['query'=>[]];
		$bid = input('get.bid');
		$db= Db::table('postpayment')->where('bid',$bid);
		if(!empty(trim(input('get.ordernumber')))){
			$ordernumber = trim(input('get.ordernumber'));
			$db->where('ordernumber',$ordernumber);
			$where['query']['ordernumber'] = $ordernumber;
		}
		$where['query']['bid'] = $bid;
		$result = $db->group('ordernumber')->paginate(8,false,$where);
		return view('order',compact('result','bid','ordernumber'));
	}
	
	/**
	 * 详情
	 * @param string get.bid 商家id
	 * @param string get.ordernumber 订单号
	*/
	public function detail()
	{
		$bid = input('get.bid');
		$ordernumber = input('get.ordernumber');
		$where['query']['bid'] = $bid;
		$where['query']['ordernumber'] = $ordernumber;
		$result = Db::table('postpayment')
				->field('postpayment.id,postpayment.number,postpayment.total,postpayment.actualtotal,
						postpayment.date,postpayment.ordernumber,postpayment.status,business.linkman,
						business.telephone,product.pname,product.purchaseprice,product.retailprice')
				->join('business','business.id = postpayment.bid','LEFT')
				->join('product','product.id = postpayment.pid','LEFT')
				->where('postpayment.status','<>',0)
				->where('postpayment.bid',$bid)
				->where('postpayment.ordernumber',$ordernumber)
				->order('id desc')
				->paginate(8,false,$where);
		return view('detail',compact('result','bid','ordernumber'));
	}
	
	/**
	 * 打款证明
	 * @param string get.id 订单号
	*/
	public function image()
	{
		$id = input('get.id');
		return view('image',compact('id'));
	}
	
	/**
	 * 上传打款证明
	 * @param string get.id 订单号
	 * @param Array
	*/
	public function playmoney()
	{
		if(request()->isAjax()){
			try{
				$id = input('post.id');
				if(!$id){
					throw new \PDOException('订单异常请联系管理员');
				}
				if(request()->file('file')){
					$image = request()->file('file');
					$path = ROOT_PATH .'public'. DS .'postpayment'. DS . $id . DS .'images';
					$imgPath = 'postpayment'. DS . $id . DS .'images';
					$info = $image->validate(['sixe'=>1567800,'ext'=>'jpg,png,gif'])->move($path);
					if($info){
						$images = $imgPath. DS .$info->getSaveName();
					}else{
						throw new \PDOException($image->getError());
					}
				}else{
					throw new \PDOException('请添加支付凭证');
				}
				$result = Db::table('postpayment')->where('id',$id)->update(['images'=>$images]);
				if(!$result){
					throw new \PDOException('支付凭证添加失败');
				}
				$success = ['success'=>'操作成功'];
				return json($success);
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	/**
	 * 订单详情
	 * @param string get.id 订单id
	 * @param string get.bid 商家bid
	*/
	public function orderDetail()
	{
		$id = input('get.id');
		$bid = input('get.bid');
		$result = Db::table('postpayment')
				->join('business','postpayment.bid = business.id','LEFT')
				->join('product','postpayment.pid = product.id','LEFT')
				->join('shop','postpayment.sid = shop.id','LEFT')
				->where('postpayment.id',$id)
				->where('postpayment.bid',$bid)
				->find();
		$result['image'] = json_decode($result['image']);
		return view('orderdetail',compact('result'));
	}
	
	/**
	 * 订单删除
	 * @param string post.id 订单id
	*/
	public function delete()
	{
		if(request()->isAjax()){
			try{
				$id = input('post.id');
				$result = Db::table('postpayment')->where('id',$id)->update(['status'=>0]);
				if(!$result){
					throw new \PDOException('删除失败');
				}
				$success = ['success'=>'删除成功'];
				return json($success);
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
}